<template>
  <el-dialog title="批量审批汰换单" :visible="visible" @close="close">
    <el-form ref="approve-form" :model="form">
      <el-form-item label="">
        <el-radio-group v-model="form.auditStatus">
          <el-radio :label="1">同意汰换</el-radio>
          <el-radio :label="0">拒绝汰换</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark" :rules="[{ required: !form.auditStatus, message: '拒绝时该项必填'}]">
        <el-input v-model="form.remark" type="textarea" maxlength="200" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="disabled" @click="close">取 消</el-button>
      <el-button v-debounce="handleAudit" :loading="loading" type="primary">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { onAudit } from '@/api/replacementApi';
export default {
  name: 'Approve',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    ids: {
      type: Array,
      default: () => {
        return [];
      }
    },
    date: {
      type: String,
      default: ''
    },
    area: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        auditStatus: 1,
        remark: ''
      },
      disabled: false,
      loading: false
    };
  },
  watch: {
    'form.auditStatus': {
      deep: true,
      handler() {
        this.$refs['approve-form'].resetFields();
      }
    }
  },
  methods: {
    // 完成关闭弹窗
    close() {
      if (!this.disabled) { this.$emit('close'); this.$refs['approve-form'].resetFields(); }
    },
    handleAudit() {
      this.$refs['approve-form'].validate(valid => {
        if (valid) {
          this.disabled = true;
          this.loading = true;
          const { auditStatus, remark } = this.form;
          onAudit(this.area, {
            auditStatus,
            ids: this.ids,
            date: this.date,
            remark
          }).then(res => {
            this.disabled = false;
            this.loading = false;
            this.$message.success('审批成功！');
            this.$emit('onApprove');
            this.close();
          }).catch(() => { this.disabled = false; this.loading = false; });
        }
      });
    }
  }
};
</script>
